﻿using APP.QUANLY.Common;
using MetroUI.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RMAS
{
    public partial class frmConfigConnect : MetroForm
    {
        public frmConfigConnect()
        {
            InitializeComponent();
            fn_LoadThongSo();
        }

        private void fn_LoadThongSo()
        {
            try
            {
                string sValue = "";
                string sServer = "", sUserName = "", sPass = "", sDatabase = "", sAutoBackup = "", sBackupCount = "";
                StaticVar_Provider _prov = new StaticVar_Provider();

                sValue = AppSetting.getSetting().getConfigValue("ip_server");
                sServer = sValue;

                sValue = AppSetting.getSetting().getConfigValue("database_server");
                sDatabase = sValue;

                sValue = AppSetting.getSetting().getConfigValue("user_server");
                sUserName = sValue;

                sValue = AppSetting.getSetting().getConfigValue("password_server");
                sPass = TCEncryption.ToDecrypt(sValue);

                sValue = AppSetting.getSetting().getConfigValue("Autobackup");
                sAutoBackup = sValue;

                sValue = AppSetting.getSetting().getConfigValue("BackupCount");
                sBackupCount = sValue;


                txtMayChu.Text = sServer;
                txtTaiKhoan.Text = sUserName;
                txtMatKhau.Text = sPass;
                txtDuLieu.Text = sDatabase;
                chbSaoLuuTuDong.Checked = bool.Parse(sAutoBackup);
                numSoFileLuu.Value = int.Parse(sBackupCount);
            }
            catch
            {
            }
        }
        private void fn_SaveThongSo()
        {
            bool bKQ;
            string sServer = "", sUserName = "", sPass = "", sDatabase = "", sAutoBackup = "", sBackupCount = "";

            sServer = txtMayChu.Text;
            sDatabase = txtDuLieu.Text;
            sUserName = txtTaiKhoan.Text;
            sPass = txtMatKhau.Text;
            sAutoBackup = chbSaoLuuTuDong.Checked.ToString();
            sBackupCount = numSoFileLuu.Value.ToString();

            if (sServer == "" || sDatabase == "" || sUserName == "" || sPass == "")
            {
                MessageBox.Show("Vui lòng điền đầy đủ thông tin");
                return;
            }
            try
            {
                AppSetting.getSetting().setConfigValue("ip_server", sServer);
                AppSetting.getSetting().setConfigValue("user_server", sUserName);
                AppSetting.getSetting().setConfigValue("password_server", TCEncryption.ToEncrypt(sPass));
                AppSetting.getSetting().setConfigValue("database_server", sDatabase);
                AppSetting.getSetting().setConfigValue("Autobackup", sAutoBackup);
                AppSetting.getSetting().setConfigValue("BackupCount", sBackupCount);

                StaticVar_Provider _prov = new StaticVar_Provider();
                bKQ = _prov.LoadServerConnection();
                if (!bKQ)
                {
                    MessageBox.Show("Thông tin cấu hình không phù hợp");
                    return;
                }
                else
                {
                    MessageBox.Show("Cấu hình kết nối thành công, chương trình sẽ tự động khởi động lại!!!");
                    Application.Restart();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            fn_SaveThongSo();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            base.Close();
        }


    }
}
